﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MatchPanel : BasePanel
{
    private Text LocalPlayerUserName;
    private Text LocalPlayerTotal;
    private Text LocalPlayerWin;

    private Text EnemyPlayerUserName;
    private Text EnemyPlayerTotal;
    private Text EnemyPlayerWin;

    private Button StartBtn;
    private Button QuitBtn;

    private UserData UserData = null;

    private CreateRoomRequest createRequest;

    private void Start()
    {
        LocalPlayerUserName = transform.Find("BluePanel/UserName").GetComponent<Text>();
        LocalPlayerTotal = transform.Find("BluePanel/TotalLabel").GetComponent<Text>();
        LocalPlayerWin = transform.Find("BluePanel/WinLabel").GetComponent<Text>();
        EnemyPlayerUserName = transform.Find("RedPanel/UserName").GetComponent<Text>();
        EnemyPlayerTotal = transform.Find("RedPanel/TotalLabel").GetComponent<Text>();
        EnemyPlayerWin = transform.Find("RedPanel/WinLabel").GetComponent<Text>();

        StartBtn = transform.Find("StartBtn").GetComponent<Button>();
        QuitBtn = transform.Find("QuitBtn").GetComponent<Button>();
        StartBtn.onClick.AddListener(OnStart);
        QuitBtn.onClick.AddListener(OnQuit);

        createRequest = GetComponent<CreateRoomRequest>();

        ClearEnemyData();
    }

    public void SetLocalPlayerResSync()
    {
        UserData = GameFacade.Instance.UserData;
    }

    public void SetLocalPlayerRes(string userName, string total, string win)
    {
        if (LocalPlayerUserName != null)
            LocalPlayerUserName.text = userName;
        if (LocalPlayerTotal != null)
            LocalPlayerTotal.text = string.Format("总场次:{0}", total);
        if (LocalPlayerWin != null)
            LocalPlayerWin.text = string.Format("胜场:{0}", win);
    }

    public void SetLocalEnemyRes(string userName, string total, string win)
    {
        if (EnemyPlayerUserName != null)
            EnemyPlayerUserName.text = userName;
        if (EnemyPlayerTotal != null)
            EnemyPlayerTotal.text = string.Format("总场次:{0}", total);
        if (EnemyPlayerWin != null)
            EnemyPlayerWin.text = string.Format("胜场:{0}", win);
    }

    private void ClearEnemyData()
    {
        if (EnemyPlayerUserName != null)
            EnemyPlayerUserName.text = string.Empty;
        if (EnemyPlayerTotal != null)
            EnemyPlayerTotal.text = "等待玩家加入...";
        if (EnemyPlayerWin != null)
            EnemyPlayerWin.text = string.Empty;
    }

    private void OnStart()
    {

    }

    private void Update()
    {
        if (UserData != null)
        {
            SetLocalPlayerRes(UserData.UserName, UserData.Total.ToString(), UserData.Win.ToString());
            ClearEnemyData();
            UserData = null;
        }
    }

    private void OnQuit()
    {
        uiMgr.PopPanel();
    }

    public override void OnEnter()
    {
        base.OnEnter();
        if(createRequest == null)
            createRequest = GetComponent<CreateRoomRequest>();
        createRequest.SendRequest();
    }

    public override void OnPause()
    {
        base.OnPause();
    }
}
